function p = payadv(rate, nper, pv, fv, adv)
%PAYADV Periodic payment given number of advance payments.
%   PAYADV returns the periodic payment given a number of advance payments.
%
%   P = payadv(RATE, NPER, PV, FV, ADV)
%
%   Inputs:
%   RATE - Scalar or vector of the lending or borrowing rate per period.
%
%   NPER - Scalar or vector of the number of periods in the life of the
%          instrument.
%
%     PV - Scalar or vector of the present value.
%
%     FV - Scalar or vector of the future value or target value to be attained
%          after NPER periods.
%
%    ADV - Scalar or vector of the number of advance payments. If the payments
%          are made at the beginning of the period, add 1 to ADV.
%
%   Outputs:
%      P - Scalar or vector of the Periodic payment.
%
%   Example:
%      The present value of a loan is $1000.00 and it will be paid in full in
%      12 months.  The annual interest rate is 10% and 3 payments are made at
%      closing time.
%
%      p = payadv(0.1/12, 12, 1000, 0, 3)
%
%      p = 85.94
%
%   See also AMORTIZE, PAYODD, PAYPER.

%       Copyright 1995-2008 The MathWorks, Inc.
%       $Revision: 1.6.2.3 $   $Date: 2008/02/02 13:04:54 $

if nargin < 5
   error('Finance:payadv:missingInputs', 'Too few inputs.')
end

if rate < 0
   error('Finance:payadv:invalidRate', 'RATE must be >= 0.')
end

c = 1+rate;
try
   p = (pv+fv.*c.^(-nper))./((1-c.^(adv-nper))./rate+adv);

catch E
   error('Finance:payadv:tryCatchError', E.message)
end


% [EOF]
